package org.study.javafx.demo12;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.image.Image;
import javafx.stage.Stage;
import lombok.Getter;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.study.javafx.demo12.model.ChinaRegion;

import java.util.List;

/**
 * 作用描述
 *
 * @author doveylovey
 * @version v1.0.0
 * @email 1135782208@qq.com
 * @date 2024年07月28日
 */
@Component
public class App extends Application implements ApplicationContextAware {
    @Getter
    private static ApplicationContext context;
    private JdbcTemplate jdbcTemplate;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        App.context = applicationContext;
    }

    @Override
    public void init() throws Exception {
        jdbcTemplate = context.getBean(JdbcTemplate.class);
    }

    @Override
    public void start(Stage stage) throws Exception {
        // 创建可观察的列表，用来存数据
        ObservableList<ChinaRegion> data = FXCollections.observableArrayList();

        // 创建表
        TableView<ChinaRegion> table = new TableView<>();
        // table.setPrefWidth(969.0);
        // table.prefHeight(620.0);
        // 创建列
        TableColumn provinceIdColumn = new TableColumn<>("省份编码");
        provinceIdColumn.setPrefWidth(100);
        TableColumn provinceNameColumn = new TableColumn<>("省份名称");
        provinceNameColumn.setPrefWidth(100);
        TableColumn cityIdColumn = new TableColumn<>("城市编码");
        cityIdColumn.setPrefWidth(100);
        TableColumn cityNameColumn = new TableColumn<>("城市名称");
        cityNameColumn.setPrefWidth(100);
        TableColumn areaIdColumn = new TableColumn<>("区域编码");
        areaIdColumn.setPrefWidth(100);
        TableColumn areaNameColumn = new TableColumn<>("区域名称");
        areaNameColumn.setPrefWidth(100);

        // 把数据放入对应的列
        provinceIdColumn.setCellValueFactory(new PropertyValueFactory<>("provinceId"));
        provinceNameColumn.setCellValueFactory(new PropertyValueFactory<>("provinceName"));
        cityIdColumn.setCellValueFactory(new PropertyValueFactory<>("cityId"));
        cityNameColumn.setCellValueFactory(new PropertyValueFactory<>("cityName"));
        areaIdColumn.setCellValueFactory(new PropertyValueFactory<>("areaId"));
        areaNameColumn.setCellValueFactory(new PropertyValueFactory<>("areaName"));
        // 把列放到表格中
        table.getColumns().addAll(provinceIdColumn, provinceNameColumn, cityIdColumn, cityNameColumn, areaIdColumn, areaNameColumn);

        // 从Spring容器中获取JdbcTemplate实例，用于执行数据库操作。执行数据库查询操作，第一个参数是SQL查询语句
        String sql = "SELECT province.province_id, province.province_name, city.city_id, city.city_name, area.area_id, area.area_name " +
                "FROM province " +
                "LEFT JOIN city ON province.province_id = city.province_id " +
                "LEFT JOIN area ON city.city_id = area.city_id " +
                "ORDER BY province.province_id, city.city_id, area.area_id ";
        List<ChinaRegion> list = jdbcTemplate.query(sql, (rs, rowNum) -> {
            // 通过ResultSet对象的getString()方法获取名为"八卦"的列的值，并赋给eightVal变量
            String provinceId = rs.getString("province_id");
            String provinceName = rs.getString("province_name");
            String cityId = rs.getString("city_id");
            String cityName = rs.getString("city_name");
            String areaId = rs.getString("area_id");
            String areaName = rs.getString("area_name");
            return new ChinaRegion(provinceId, provinceName, cityId, cityName, areaId, areaName);
        });
        for (ChinaRegion chinaRegion : list) {
            data.add(new ChinaRegion(chinaRegion.getProvinceId(), chinaRegion.getProvinceName(), chinaRegion.getCityId(), chinaRegion.getCityName(), chinaRegion.getAreaId(), chinaRegion.getAreaName()));
        }
        // 把数据放入表中
        table.setItems(data);

        stage.setWidth(650);
        stage.setHeight(500);
        stage.getIcons().add(new Image("/image/icon.jpg"));
        stage.setTitle("SpringBoot3+JavaFX示例程序");
        stage.setScene(new Scene(table));
        stage.show();
    }
}
